//Create global namespace Happy
if (typeof Happy == "undefined" || !Happy) {
  var Happy = {};
}

//Module for $=jQuery
(function($) {
//Create namespace Happy.WidgetExpand
//Make widgets expandable
//Title (click to toggle content): h2:first
//Content: div.widget-content
//Prepend span.widget-expand to title
Happy.WidgetExpand = (function(){
	//Public members
  return {
		//Widgets to be expandable. Need be Object with two properties.
		//css: selector string of the widget
		//close: boolean for widget content hide or show
    TARGET_WIDGET: [],
		//Symbol for expandable widget
    OPEN_SYMBOL: '[-]',
    CLOSE_SYMBOL: '[+]',
    
		//Make target widgets expandable
    attachIt: function() {
      var we = Happy.WidgetExpand;
      $(we.TARGET_WIDGET).each(function(i,target){
			  //Create expandable symbol and hide/show the widget content
        var expand = $('<span class="widget-expand"></span>');
        if (target.close) {
          expand.html(we.CLOSE_SYMBOL).addClass('expand-close');
          $(target.css + " div.widget-content").hide();
        } else {
          expand.html(we.OPEN_SYMBOL).addClass('expand-open');
          $(target.css + " div.widget-content").show();
        }
				//Click widget title to toggle conent
        $(target.css + " h2").eq(0).prepend(expand).bind('click', function(){
          if ($(target.css + " div.widget-content").is(':hidden')) {
            $(target.css + " span.widget-expand").html(we.OPEN_SYMBOL).removeClass('expand-close').addClass('expand-open');
          } else {
            $(target.css + " span.widget-expand").html(we.CLOSE_SYMBOL).removeClass('expand-open').addClass('expand-close');
          }
          $(target.css + " div.widget-content").toggle();
        });
      });//end of each
    }//end of function
  };//end of return
})();
})(jQuery);